package com.test;

public class JsonWriter {
	
	Context ctx = new Context();
	
	public void enterArray(){
		System.out.print("[");
		ctx.scops.push(new Scope());
	}
	
	public void exitArray(){
		System.out.print("]");
		ctx.scops.pop();
	}
	
	
	public void enterObj(){
		System.out.print("{");
		ctx.scops.push(new Scope());
	}
	
	public void exitObj(){
		System.out.print("}");
		ctx.scops.pop();
	}
	

	
	public void writeKey(JsonKey key){
		checkVisit();
		if(key!=null){
			if(key.isIndex()){
				
			}else{
				System.out.print(key.getName()+":");
			}
			
		}
		
	}
	
	public void writeValue(Object value){
		if(value instanceof DirectValue){
			System.out.print(value);
		}else  if(value instanceof String){
			
			System.out.print("\""+value+"\"");
		}else{
			
			System.out.print(value);
		}
		
	}
	
	public void writeKeyValue(JsonKey key,Object value){
		
		writeKey(key);
		writeValue(value);
	}

	protected void checkVisit(){
		if(ctx.isVisit()){
			
			System.out.print(",");
			
		}else{
			ctx.setVisit();
			return ;
		}
	}

	public Context getCtx() {
		return ctx;
	}

	public void setCtx(Context ctx) {
		this.ctx = ctx;
	}
	
	public static class  DirectValue{
		public String str;
		public DirectValue(String str){
			this.str = str;
		}
	}
	
}
